package com.hong.service_product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.hong.fy_common_commomutil.common.result.Result;
import com.hong.fy_model.model.review.ReviewEntity;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;



import com.hong.service_product.service.ReviewService;



/**
 * 
 *
 * @author wqh
 * @email 1601709391@qq.com
 * @date 2022-06-15 20:37:30
 */
@Api(tags = "评论")
@RestController
@RequestMapping("product/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;


    @ApiOperation(value = "用户评论接口")
    @PostMapping("/reply")
    public Result reply(@RequestBody ReviewEntity review){
        Boolean flag = reviewService.reply(review);
        if (flag) {
            return Result.ok("评论成功");
        }
        return Result.fail("评论失败");
    }

    @ApiOperation(value = "根据评论id删除评论")
    @DeleteMapping("/delete")
    public Result deleteReview(@RequestParam(value = "reviewId") String  reviewId){
        Boolean flag = reviewService.deleteReview(reviewId);
        if (flag) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除评论失败");

    }


    @ApiOperation(value = "根据商品id分页查询评论")
    @GetMapping("/listByProductId")
    public Result listByProductId(@RequestParam(value = "productId" ,required = true) String productId,
                                 @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        IPage<ReviewEntity> list = reviewService.listByProductId(productId, pageNum, limit);
        //如果没有数据，则提示暂无评论
        if(list.getRecords().size() == 0){
            return Result.ok("暂无评论");
        }
        return Result.ok(list);
    }

    @ApiOperation(value = "评论点赞")
    @PostMapping("/like")
    public Result like(@RequestParam(value = "reviewId") String reviewId){
        Boolean flag = reviewService.like(reviewId);
        if (flag) {
            return Result.ok("点赞成功");
        }
        return Result.fail("点赞失败");
    }

}
